﻿using System;

using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace Token.Piker.Web.Admin
{
    public partial class ImageOperation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateThumbnail();
        }

        //剪切图片
        private void Cut()
        {
            //根据指定的宽度和高度剪切指定图片
            string path = Request["path"];
            int width = int.Parse(Request["width"]);
            int height = int.Parse(Request["height"]);
            //根据图片路径获得要剪切的图片
            System.Drawing.Image srcImage = System.Drawing.Image.FromFile(path);
            //获取源图的尺寸
            int srcWidth = srcImage.Width;
            int srcHeight = srcImage.Height;
            //计算图片剪切的起点坐标
            int startX = 0;
            int startY = 0;
            startX = (srcWidth - width) / 2;
            startY = (srcHeight - height) / 2;
            Bitmap srcBitmap = new Bitmap(srcImage);
            Bitmap bitmap = Token.Piker.Common.ImageOperation.Cut(srcBitmap, startX, startY, width, height);
            //将图片输出到页面上
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());
        }

        //生成缩略图
        private void CreateThumbnail()
        {
            string path = Request["path"];
            int width = int.Parse(Request["width"]);
            int height = int.Parse(Request["height"]);
            path = Server.MapPath(path);
            System.Drawing.Image srcImage = System.Drawing.Image.FromFile(path);
            Bitmap srcBitmap = new Bitmap(srcImage);
            Bitmap bitmap = Token.Piker.Common.ImageOperation.ResizeImage(srcBitmap,width, height);
            //将图片输出到页面上
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
    }
}